﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace guosange.Common
{
    public static class ObjectExtension
    {
        ///// <summary>
        ///// Used to simplify and beautify casting an object to a type. 
        ///// </summary>
        ///// <typeparam name="T">Type to be casted</typeparam>
        ///// <param name="obj">Object to cast</param>
        ///// <returns>Casted object</returns>
        //public static T As<T>(this object obj) where T : class
        //{
        //    return (T)obj;
        //}

        //public static T Convert<T>(this object obj) where T : struct
        //{
        //    return (T)System.Convert.ChangeType(obj, typeof(T));
        //}

        ///// <summary>
        ///// Check if an item is in a list.
        ///// </summary>
        ///// <param name="item">Item to check</param>
        ///// <param name="list">List of items</param>
        ///// <typeparam name="T">Type of the items</typeparam>
        //public static bool IsIn<T>(this T item, params T[] list)
        //{
        //    return list.Contains(item);
        //}


        //public static string ToJson(this object obj, PropertyNameResolver resolver = PropertyNameResolver.Default, bool isNullValueHandling = false, bool indented = false, string DateTimeFormat = "yyyy-MM-dd HH:mm:ss")
        //{
        //    var options = new JsonSerializerSettings();

        //    switch (resolver)
        //    {
        //        case PropertyNameResolver.CamelCase:
        //            options.ContractResolver = new CamelCasePropertyNamesContractResolver();
        //            break;
        //        case PropertyNameResolver.LowerCase:
        //            options.ContractResolver = new LowerCaseContractResolver();
        //            break;
        //    }
        //    if (indented)
        //        options.Formatting = Formatting.Indented;
        //    if (isNullValueHandling)
        //        options.NullValueHandling = NullValueHandling.Ignore;
        //    options.Converters = new List<JsonConverter>() { new IsoDateTimeConverter { DateTimeFormat = DateTimeFormat } };
        //    return JsonConvert.SerializeObject(obj, options);
        //}

        //public static string ToDateTimeString(this object obj, string format)
        //{
        //    DateTime dateTime = new DateTime();
        //    DateTime.TryParse(obj?.ToString(), out dateTime);
        //    return dateTime.ToString(format);
        //}

        //public static bool ToBool(this object obj, bool defaultResult = false)
        //{
        //    bool.TryParse(obj?.ToString(), out defaultResult);
        //    return defaultResult;
        //}
    }
}
